package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.common.utils.MyPageHelper;
import com.project.entity.Notice;
import com.project.entity.User;
import com.project.myBeans.R;
import com.project.myBeans.ResponseBean;
import com.project.service.NoticeService;
import com.project.service.UserService;
import com.project.service.base.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/27 15:01
 * @param:
 * @return:
 */
@EnableAutoConfiguration //自动载入应用程序所需的所有Bean
@RestController //@Controller与@ResponseBody的合并
@RequestMapping(value = "/notice")
@Api(description = "通知操作",value = "notice")
public class NoticeController extends BaseService {
    @Autowired
    private NoticeService noticeService;

    @Autowired
    private UserService userService;

    /**
     * TateBrown 2018年5月27日
     *
     * @param notice
     * @return
     */
    @ApiOperation("分页条件查找")
    @PostMapping("/findByCondition")
    public R findByCondition(@RequestBody Notice notice) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (notice.getPageIndex() > 0)
                notice.setPageIndex(notice.getPageIndex() - 1);
            List<Notice> notices = noticeService.findByCondition(notice);
            return R.ok("获取成功").put("data",notices).put("total", MyPageHelper.getTotal(notices));
        } catch (Exception e) {
            return R.error("程序异常");
        }

    }

    /**
     * TateBrown 2018年5月27日
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查找通知信息")
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable("id") Integer id) {
        JSONObject jsonObject = new JSONObject();
        User user;
        try {
            Notice notice = noticeService.findById(id);
            jsonObject.put("notice", notice);
            if (notice != null) {
                user = userService.findOneByUserID(notice.getComposerid());
                if (user != null) {
                    jsonObject.put("composerName", user.getRealname());
                }
                return R.ok("查找成功").put("data",jsonObject);
            }
            else {
                return R.error("该通知不存在");

            }
        } catch (Exception e) {
            return R.error("程序异常");

        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id删除通知信息")
    @GetMapping("/removeById/{id}")
    public R removeById(@PathVariable("id") Integer id) {
        try {
            if(processSuccessDeleteUpdate(noticeService.delete(id))){
                return R.ok("删除成功");
            }else{
                return R.error("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param notice
     * @return
     */
    @ApiOperation("添加通知")
    @PostMapping("/add")
    public R add(@RequestBody Notice notice) {
        try {
            noticeService.add(notice);
            return R.ok("发送成功");

        } catch (Exception e) {
           return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param notice
     * @return
     */
    @ApiOperation("根据通知表主键修改通知信息")
    @PostMapping("/modifyOneById")
    public R modifyOneById(@RequestBody Notice notice) {
        try {
            if (checkDeleteUpdateIsSuccess(noticeService.modify(notice))) {
                return R.ok("修改成功");
            }
            else {
                return R.error("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }


}
